// 业务城市加载
window.onload = function(){
    var url = 'http://192.168.0.108:8080/AppverManage/getAllAppVerByPage'
    function s(datas) {
        function operateFormatter(value, row, index) {//赋予的参数
            return [
                '<button class="btn btn-primary" style="margin-right:10px"  onclick="editApp(' + row.appverid + ')">编辑</button>',
            ].join('');
        }
         // 设置table列样式
         function  formatFormatter(value, row, index) {
            if (value == 1){
            
            return [
               
                '<img src="../../../images/success.png" onclick="statusChange(' + row.mainflag+','+row.appverid + ')">',
            ].join('');
        }else{
            return [
               
                '<img src="../../../images/fail.png" onclick="statusChange(' + row.mainflag +','+row.appverid + ')">',
            ].join('');
        }
        }
        var tableColumns = [
            {
                field: 'appverid',
                title: 'id',
                switchable: true
            }, {
                field: 'platformflag',
                title: '平台',
                switchable: true
            },{
                field: 'role',
                title: '使用角色',
                switchable: true
            },
            {
                field: 'ver',
                title: '版本号',
               // formatter: formatFormatter
            }, {
                field: 'vcode',
                title: 'versionCode',
                //formatter: operateFormatter //自定义方法，添加操作按钮
            },
            {
                field: 'downurl',
                title: '下载地址',
                //formatter: operateFormatter //自定义方法，添加操作按钮
            },
            {
                field: 'updatelog',
                title: '更新日志',
                //formatter: operateFormatter //自定义方法，添加操作按钮
            },
            {
                field: 'mainflag',
                title: '当前发行中版本',
                formatter: formatFormatter //自定义方法，添加操作按钮
            },
            {
                field: 'operate',
                title: '操作',
                formatter: operateFormatter //自定义方法，添加操作按钮
            }

        ];
        $("#appTable").bootstrapTable('destroy'); //分页必须写
        $("#appTable").bootstrapTable({
            cache: false,
            pagination: true,
            sidePagination: "client",  //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,   //初始化加载第一页，默认第一页
            pageSize: 5,   //每页的记录行数（*）
            pageList: [5, 10], //可供选择的每页的行数
            columns: tableColumns,
            data: datas,
            queryParamsType: '',
            //得到查询的参数
            queryParams: function queryParams(params) {
                return params;
            },
            onLoadSuccess: function (data) {  //加载成功时执行
                alert("成功" + data)
            },
            onLoadError: function () {  //加载失败时执行
                alert("失败")
            }

        });
    }
    $.ajax({
        url: url,
        method : 'post',
        contentType: "application/x-www-form-urlencoded",
        success: function (res) {
            console.log(res.data)
            s(res.data)
           
        }
    })
}
// 点击添加
function addApp(e) {
    // 添加
    divset = document.getElementsByClassName("search");
    for (var i = 0; i<divset.length;i++) {
         divset[i].style.display="none";
    };
    divsetadd = document.getElementsByClassName("addApp");
    for (var i = 0; i<divset.length;i++) {
       divsetadd[i].style.display="inline";
    };
}
// 点击添加保存
function addsaveApp(e) {
    // 获取要传的值
    var adddownUrl = document.getElementById("adddownUrl").value
    var addplatformFlag = $("#addplatformFlag option:selected").val()
    var addrole = $("#addrole option:selected").val()
    var addupdateLog = document.getElementById("addupdateLog").value
    var addver = document.getElementById("addver").value
    var addversionCode = document.getElementById("addversionCode").value
    var date = {
        downUrl : adddownUrl,
        platformFlag : addplatformFlag,
        role : addrole,
        updateLog : addupdateLog,
        ver : addver,
        versionCode : addversionCode
    }
    var  datas = JSON.stringify(date)
    console.log(addrole)
    $.ajax({
        url: "http://192.168.0.108:8080/AppverManage/addAppVer",
        method:"post",
        contentType:"application/json",
        data:datas,
        success: function (res) {
            console.log(res.data)
        },
    })
     // 返回
   divset = document.getElementsByClassName("search");
   for (var i = 0; i<divset.length;i++) {
        divset[i].style.display="inline";
        location.reload();
   };
   divsetedit = document.getElementsByClassName("addApp");
   for (var i = 0; i<divset.length;i++) {
       divsetedit[i].style.display="none";
   };
   divsetadd = document.getElementsByClassName("editApp");
   for (var i = 0; i<divset.length;i++) {
       divsetadd[i].style.display="none";
   };
}
function backApp(e) {
   // 返回
   divset = document.getElementsByClassName("search");
   for (var i = 0; i<divset.length;i++) {
        divset[i].style.display="inline";
   };
   divsetedit = document.getElementsByClassName("addApp");
   for (var i = 0; i<divset.length;i++) {
       divsetedit[i].style.display="none";
   };
   divsetadd = document.getElementsByClassName("editApp");
   for (var i = 0; i<divset.length;i++) {
       divsetadd[i].style.display="none";
   };
}
// 点击编辑按钮
function editApp(appverid) {
     // 编辑赋值给编辑列表
     $.ajax({
        url: 'http://192.168.0.108:8080/AppverManage/getEditAppverInfo',
        method: 'post',//请求方式
        contentType: "application/x-www-form-urlencoded",
        data:{
            appVerID:appverid
        },
        success: function (res) {
            console.log(res)
            // 赋值APP平台
            $("#editplatformFlag").find("option[value='"+res.data.platformflag+"']").attr("selected",true);
            $("#editrole").find("option[value='"+res.data.role+"']").attr("selected",true);
            
            // 编辑版本号
             $("#editver").val(res.data.ver);
            //  编辑的code
            $("#editversionCode").val(res.data.vcode);
            // 地址
            $("#editdownUrl").val(res.data.downurl)
        //    更新地址
            $("#editupdateLog").val(res.data.updatelog)
        // id
            $("#appid").val(res.data.appverid)
        }
   })
  // 编辑
  divset = document.getElementsByClassName("search");
  for (var i = 0; i<divset.length;i++) {
       divset[i].style.display="none";
  };
  divsetedit = document.getElementsByClassName("editApp");
  for (var i = 0; i<divset.length;i++) {
   divsetedit[i].style.display="inline";
  };
}
// 编辑保存按钮
function editsaveApp(e) {
    // 获取要传的值
    var editappVerID = document.getElementById("appid").value
    var editdownUrl = document.getElementById("editdownUrl").value
    var editplatformFlag = $("#editplatformFlag option:selected").val()
    var editrole = $("#editrole option:selected").val()
    var editupdateLog = document.getElementById("editupdateLog").value  
    var editver = document.getElementById("editver").value
    var editversionCode = document.getElementById("editversionCode").value
    var date = {
        appVerID : editappVerID,
        downUrl : editdownUrl,
        platformFlag : editplatformFlag,
        role :editrole,
        updateLog :editupdateLog,
        ver :editver,
        versionCode : editversionCode
    }
    var datas = JSON.stringify(date)
    $.ajax({
        url : "http://192.168.0.108:8080/AppverManage/updateAppver",
        method : "post",
        contentType : "application/json",
        data:datas,
        success: function (res) {
            console.log(res.msg)
        }
    })
         // 返回
   divset = document.getElementsByClassName("search");
   for (var i = 0; i<divset.length;i++) {
        divset[i].style.display="inline";
        location.reload();
   };
   divsetedit = document.getElementsByClassName("addApp");
   for (var i = 0; i<divset.length;i++) {
       divsetedit[i].style.display="none";
   };
   divsetadd = document.getElementsByClassName("editApp");
   for (var i = 0; i<divset.length;i++) {
       divsetadd[i].style.display="none";
   };
}
// 切换状态
// 当前发行版本状态
function statusChange(mainflag,appid) {
    // 获取当前状态
    console.log(mainflag);
    // 判断当前状态更改
    if(mainflag === 1){
       $('#modal-input').html('确认停用');
       $('#myModal').modal('show');
        var appstatus=0
        console.log("dianji"+appstatus+appid);
        changeStata(appstatus,appid)
    }
    else{
       $('#modal-input').html('确认启用');
       $('#myModal').modal('show');
        var appstatus=1
        console.log("dianji"+appstatus+appid);
        changeStata(appstatus,appid)
    }
}
// 品牌状态更改按钮
// 切换状态
function changeStata(appstatus,appid) {
    $('#btn_ok').click(function(){
    $.ajax({
        url: 'http://192.168.0.108:8080/AppverManage/updateMainFlag',
        method:'post',
        contentType:'application/x-www-form-urlencoded',
        data:{
            appverID:appid,
            mainFlag:appstatus
        },
         success: function (res) {
            console.log(res.msg);
            $('#myModal').modal('hide');
            location.reload();
         }
    })
})
}